#include "widget.h"
#include "ui_widget.h"
#include "label.h"
#include <QKeyEvent>
#include <QDebug>
#include <QWheelEvent>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 设置鼠标追踪状态，才能实现获取鼠标位置，方便实现mouseMoveEvent函数
    setMouseTracking(true);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::keyPressEvent(QKeyEvent *event)
{
    // 组合按键
    if(event->modifiers() == Qt::ControlModifier)
    {
        // 单个按键
        if(event->key() == Qt::Key_G)
        {
            qDebug() << "按下Ctrl + G键";
        }
    }

}

void Widget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        qDebug() << "按下左键";
    }
    else if(event->button() == Qt::RightButton)
    {
        qDebug() << "按下右键";
    }
    else if(event->button() == Qt::MidButton)
    {
        qDebug() << "按下滚轮";
    }
    else
    {
        qDebug() << "按下其他扩展按键";
    }
    // QString的格式化写入方法
    //QString str1 = QString("鼠标按下的位置，x:%1 y:%2").arg(event->x()).arg(event->y());
    //qDebug() << str1;
}

void Widget::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        qDebug() << "释放左键";
    }
    else if(event->button() == Qt::RightButton)
    {
        qDebug() << "释放右键";
    }
    else if(event->button() == Qt::MidButton)
    {
        qDebug() << "释放滚轮";
    }
    else
    {
        qDebug() << "释放其他扩展按键";
    }
}

void Widget::mouseDoubleClickEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        qDebug() << "双击左键";
    }
    else if(event->button() == Qt::RightButton)
    {
        qDebug() << "双击右键";
    }
    else if(event->button() == Qt::MidButton)
    {
        qDebug() << "双击滚轮";
    }
    else
    {
        qDebug() << "双击其他扩展按键";
    }
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    qDebug() << "[" << event->x() << "," << event->y() << "]";
}

void Widget::wheelEvent(QWheelEvent *event)
{
    static int x = 0;

    x += event->delta(); // +-120

    if (event->delta() > 0)
    {
        qDebug() << "向前滚动 " << x;
    }
    else
    {
        qDebug() << "向后滚动 " << x;
    }
}

